package com.yifang.yibingproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifang.yibingproject.entity.SysRoleEntity;
import com.yifang.yibingproject.entity.SysUserRoleEntity;
import com.yifang.yibingproject.mapper.SysUserRoleMapper;
import com.yifang.yibingproject.service.SysRoleService;
import com.yifang.yibingproject.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {


    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 根据用户ID获取权限集合
     * @param userId
     * @return
     */
    @Override
    public List<SysRoleEntity> listRolesByUserId(Long userId) {
        if(userId == null){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysUserRoleEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRoleEntity::getUserId,userId);
        List<SysUserRoleEntity> userRoleList = list(lambdaQueryWrapper);

        List<Long> roleIds = userRoleList.stream().map(SysUserRoleEntity::getRoleId).collect(Collectors.toList());
        List<SysRoleEntity> roleList = sysRoleService.getListByRoleIds(roleIds);
        return roleList;
    }

    /**
     * 根据用户ID  获取角色ID集合
     * @param userId
     * @return
     */
    @Override
    public Set<Long> listRoleIdsByUserId(Long userId) {
        List<SysRoleEntity> list = listRolesByUserId(userId);
        Set<Long> result = list.stream().map(SysRoleEntity::getId).collect(Collectors.toSet());
        return result;
    }

    /**
     * 根据用户ID获取集合
     * @param userId
     * @return
     */
    @Override
    public List<SysUserRoleEntity> selectListByUserId(Long userId) {
        if(userId == null || userId <= 0){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<SysUserRoleEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysUserRoleEntity::getUserId,userId);
        List<SysUserRoleEntity> list = list(queryWrapper);
        return list;
    }

    /**
     * 安全的使用 id集合获取 实体
     * @param ids
     * @return
     */
    @Override
    public List<SysUserRoleEntity> safetyGetListByRoleIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<SysUserRoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id",ids);
        List<SysUserRoleEntity> list = list(queryWrapper);
        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        }
        return list;
    }
}
